package com.theydream.qeechain.battle;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import android.util.Log;

import com.theydream.qeechain.globaldata.GlobalData;
import com.theydream.qeechain.globaldata.definition.BattleQee;
import com.theydream.qeechain.globaldata.definition.Countable;
import com.theydream.qeechain.globaldata.definition.Effect;
import com.theydream.qeechain.globaldata.definition.Modifier;
import com.theydream.qeechain.globaldata.definition.Qee;


public class BattleLogic {
	
	private SequenceLogic seq;
	
	// Constructor
	public BattleLogic()	{
//		seq = new SequenceLogic();
	}
	
	// Load and create battle data
	public void loadBattleData(List<String> allies, List<String> enemies)	{
		
		GlobalData.getInstance().getPetListBattle().clear();
		// Load all pets to be controlled by the user
		//TODO: other pets should not have same skill as user
		int i=0;
		Set<String> skill = GlobalData.getInstance().getDbhelper().getUserSkill();
		Log.i("InUsePet", Integer.toString(GlobalData.getInstance().getPetInUse().size()));
		for (Qee userQee : GlobalData.getInstance().getPetInUse())	{
			int hp = userQee.getStat().get("HP");
			int ap = userQee.getStat().get("AP");
			Set<Countable> consumables = new HashSet<Countable>();
			List<Modifier> mod = new ArrayList<Modifier>(); 
			
			BattleQee battleQee = new BattleQee(1, hp, ap, 1, skill, consumables, mod, userQee);
			GlobalData.getInstance().getPetListBattle().put(Integer.toString(i), battleQee);
			i++;
		}
//		Log.i("TEST", Integer.toString(i));
		//TODO handle ally
		
		// Create Enemy
		for (String epid : enemies)	{
		Log.i("epid", epid);
			Qee eQee = GlobalData.getInstance().getPetList().get(epid);
			int hp = eQee.getStat().get("HP");
			int ap = eQee.getStat().get("AP");
			Set<Countable> consumables = new HashSet<Countable>();
			List<Modifier> mod = new ArrayList<Modifier>();
			
			BattleQee battleQee = new BattleQee(3, hp, ap, 1, skill, consumables, mod, eQee);
			GlobalData.getInstance().getPetListBattle().put(Integer.toString(i), battleQee);
			i++;
		}
		Log.i("TEST", Integer.toString(i));
	}
	
	// Load image for pets
	public List<String> getPetImageId()	{
		List<String> pic = new ArrayList<String>();
//		Log.i("BattleListSize", Integer.toString(GlobalData.getInstance().getPetListBattle().size()));
		for (Map.Entry<String, BattleQee> entry : GlobalData.getInstance().getPetListBattle().entrySet())	{
			pic.add(entry.getValue().getQee().getPic());
		}
		
		return pic;
	}
	
	// Initialize move sequence
	public void initialSequence()	{
//		for (Map.Entry<String, BattleQee> entry : GlobalData.getInstance().getPetListBattle().entrySet())	{
		seq = new SequenceLogic();

	}

	// Handle the skill selected by user in a user selection move
	public void handleUserMove(Effect newEffect)	{
		Move userMove = seq.getNextMove();
		userMove.reset();
		userMove.increment(newEffect.getDelay());
		seq.addMove(userMove);
		seq.addEffectReleaseMove("0", newEffect);
	}
	
	// Handle skill/consumable release
	public void handleRelease()	{
		Move releaseMove = seq.getNextMove();
		String effect = releaseMove.getEffect().getEffect();
//		int dmg = Integer.valueOf(effect.split(";")[4]);
//		String targetId=null;
//		if (0>dmg)	{
//			targetId = releaseMove.getPetID();
//		}
//		else	{
//			targetId = getTargetId(releaseMove.getPetID());
//		}	
		
		int hp = GlobalData.getInstance().getPetListBattle().get(getTargetId(releaseMove.getPetID())).getHp();		
		hp -= Integer.valueOf(effect.split(";")[4]);
//		Log.i("HP", Integer.toString(hp));
		if (hp<0)	{
			hp = 0;
		}
		GlobalData.getInstance().getPetListBattle().get(getTargetId(releaseMove.getPetID())).setHp(hp);	
	}
	
	// Let the computer choose a skill
	public void handleCompMove()	{
		Move compMove = seq.getNextMove();
		compMove.reset();
		Effect newSkill = GlobalData.getInstance().getSkills().get("423");
		Log.i("Skill_count", Integer.toString(GlobalData.getInstance().getSkills().size()));
		compMove.increment(newSkill.getDelay());
		seq.addMove(compMove);
		seq.addEffectReleaseMove("1", newSkill);
	}
	
	// temporary: get target id
	public String getTargetId(String skillFrom)	{
		if ("0".equals(skillFrom))	
			return "1";

		return "0";
	}

	// Return whether the next move is a User skill select(0,0)
	// or Computer skill select (1,0)/User Skill Release(1,0)/ Computer Skill Release(1,1)
	public String getNextPlayerType()	{	
		return seq.peekNextMove().getPetID() + "," + seq.peekNextMove().getType();
	}
	
	public Effect getNextEffect()	{
		return seq.peekNextMove().getEffect();
	}
}